package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.service.CatalogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

public class SearchServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        String keyword = request.getParameter("keyword");
        System.out.println(keyword);
        CatalogService service = new CatalogService();
        //name = new String(name.getBytes("iso-8859-1"),"utf-8");
        List<Product> pList = service.getSearchInfo(keyword);
        String res = "";
        System.out.println(pList.size());
        for (int i = 0; i<pList.size();i++) {
            res += "," + pList.get(i).getName();
        }
        PrintWriter out = response.getWriter();
        out.write(res);
        out.flush();
        out.close();
    }
}
